package com.lsl.myfilemanager.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.lsl.myfilemanager.R;
import com.lsl.myfilemanager.bean.FileInfo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * author lishilin
 * date 2023/8/10
 * desc 文件扫描类，包括五个扫描方法，扫描不同类型的文件
 */
public class FileScanUtil {

    /**
     * 扫描全sdcard文件
     * @param directoryPath
     * @return
     */
    public static List<FileInfo> scanRootFiles(String directoryPath) {
        List<FileInfo> fileList = new ArrayList<>();
        File directory = new File(directoryPath);
        if (directory.exists()) {
            File[] files = directory.listFiles();

            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        FileInfo fileInfo = new FileInfo();
                        fileInfo.setFileName(file.getName());
                        fileInfo.setPath(file.getAbsolutePath());
                        fileInfo.setType(FileTypeUtil.DIRECTORY);
                        fileInfo.setFileIconResourceId(R.drawable.file);
                        fileList.add(fileInfo);
                    } else {
                        FileInfo fileInfo = new FileInfo();
                        fileInfo.setFileName(file.getName());
                        fileInfo.setPath(file.getAbsolutePath());
                        String fileType = getFileType(file);
                        if (fileType == null) {
                            fileInfo.setType(FileTypeUtil.UNKNOWN);
                            fileInfo.setFileIconResourceId(R.drawable.unknownfile);
                        } else {
                            if (fileType.equals("audio/mpeg")) {
                                fileInfo.setType(FileTypeUtil.MUSIC);
                                fileInfo.setFileIconResourceId(R.drawable.music_file);
                            } else if (fileType.equals("image/jpeg") || fileType.equals("image/png")) {
                                fileInfo.setType(FileTypeUtil.PICTURE);
                                fileInfo.setFileIconResourceId(R.drawable.picture_file);
                            } else if (fileType.equals("video/mp4")) {
                                fileInfo.setType(FileTypeUtil.VIDEO);
                                fileInfo.setFileIconResourceId(R.drawable.video_file);
                            } else if (getExtension(file.getName()).equals("apk")) {
                                fileInfo.setType(FileTypeUtil.APK);
                                fileInfo.setFileIconResourceId(R.drawable.apk);
                            } else if (getExtension(file.getName()).equals("pdf") || getExtension(file.getName()).equals("doc") || getExtension(file.getName()).equals("txt")
                            || getExtension(file.getName()).equals("docx")) {
                                fileInfo.setType(FileTypeUtil.UNKNOWN);
                                fileInfo.setFileIconResourceId(R.drawable.document);
                            }else{
                                fileInfo.setType(FileTypeUtil.UNKNOWN);
                                fileInfo.setFileIconResourceId(R.drawable.unknownfile);
                            }
                        }
                        fileList.add(fileInfo);
                    }
                }
            }
        }
        return fileList;
    }

    /**
     * 扫描图片
     * date 2023/8/11
     */
    public static List<FileInfo> scanImageFiles(Context context) {
        List<FileInfo> ImageFiles = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor mAudioCursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                null
        );
        for (int i = 0; i < mAudioCursor.getCount(); i++) {
            mAudioCursor.moveToNext();
            String path = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
            String ImageName = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
            FileInfo fileInfo = new FileInfo();
            fileInfo.setPath(path);
            fileInfo.setFileName(ImageName);
            fileInfo.setType(FileTypeUtil.PICTURE);
            fileInfo.setFileIconResourceId(R.drawable.picture_file);
            ImageFiles.add(fileInfo);
        }
        mAudioCursor.close();
        return ImageFiles;
    }

    public static List<FileInfo> scanAudioFiles(Context context) {
        List<FileInfo> musicFiles = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor mAudioCursor = contentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                null
        );
        for (int i = 0; i < mAudioCursor.getCount(); i++) {
            mAudioCursor.moveToNext();
            String path = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            String musicName = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            FileInfo fileInfo = new FileInfo();
            fileInfo.setPath(path);
            fileInfo.setFileName(musicName);
            fileInfo.setType(FileTypeUtil.MUSIC);
            fileInfo.setFileIconResourceId(R.drawable.music_file);
            musicFiles.add(fileInfo);
        }
        mAudioCursor.close();
        return musicFiles;
    }

    public static List<FileInfo> scanVideoFiles(Context context) {
        List<FileInfo> videoFiles = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor mAudioCursor = contentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                null
        );
        for (int i = 0; i < mAudioCursor.getCount(); i++) {
            mAudioCursor.moveToNext();
            String path = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            String ImageName = mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
            FileInfo fileInfo = new FileInfo();
            fileInfo.setPath(path);
            fileInfo.setFileName(ImageName);
            fileInfo.setType(FileTypeUtil.VIDEO);
            fileInfo.setFileIconResourceId(R.drawable.video_file);
            videoFiles.add(fileInfo);
        }
        mAudioCursor.close();
        return videoFiles;
    }
    
    public static List<FileInfo> getApkFile(String path) {
        List<FileInfo> apks = new ArrayList<>();
        File directory = new File(path);
        if (directory.exists()) {
            File[] files = directory.listFiles();
            if (files != null){
                for (File file : files){
                    if (file.isDirectory()){
                        //递归扫描全部文件
                        apks.addAll(getApkFile(file.getAbsolutePath()));
                    }else{
                        if (file.getName().endsWith(".apk")){
                            FileInfo info = new FileInfo();
                            info.setFileIconResourceId(R.drawable.apk);
                            info.setFileName(file.getName());
                            info.setType(FileTypeUtil.APK);
                            info.setPath(file.getPath());
                            apks.add(info);
                        }
                    }
                }
            }
        }
        return apks;
    }

    private static String getFileType(File file) {
        try {
            return Files.probeContentType(file.toPath());
        } catch (IOException e) {
            e.printStackTrace();
            return "Unknown";
        }
    }

    /**
     * 提取文件后缀名
     *
     * @param path
     * @return
     */
    public static String getExtension(String path) {
        if (path == null) {
            return null;
        }
        int extensionIndex = path.lastIndexOf('.');
        if (extensionIndex == -1) {
            return null;
        }
        return path.substring(extensionIndex + 1);
    }
}
